﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;

namespace hbse
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        private readonly bool isStandAlone = true;
        private ISession session;
    
        public virtual Users User { get; set; }

        private Ussimplecustomer _ussimplecustomer;
        private Usfloderproduct _usfloderproduct;
        private Uscustomer _uscustomer;
        private Ussimplefloder _ussimplefloder;
        private Usoutgoing _usoutgoing;
        private Uscoa _uscoa;
        private Usaddorder _usaddorder;
        private Usprintorder _usprintorder;
        private Uscore _uscore;
        private Uspacking _uspacking;


        public frmMain()
        {
            InitializeComponent();
            if (session == null || !session.IsOpen)
            {
                if (session != null)
                {
                    session.Dispose();
                }
                session = ApplicationContext.SessionFactory.OpenSession();
                session.FlushMode = FlushMode.Never;
            }

           var _user = session.CreateQuery("from Users u where UserName='admin' ").Future<Users>();
           if (_user.Count() > 0)
           {
               User = (Users)_user.First();
           }
        }

       
        public bool IsStandAlone
        {
            get { return isStandAlone; }
        }
         
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            int CurrentWidth = Screen.PrimaryScreen.Bounds.Width;
            int CurrentHeight = Screen.PrimaryScreen.Bounds.Height;
        }

        private void buttonItem23_Click(object sender, EventArgs e)
        {

            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Simplecustomer";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Simplecustomer");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _ussimplecustomer = new Ussimplecustomer(tabControl2, newTab, session,User);

            panel.Controls.Add(_ussimplecustomer);
            tabControl2.SelectedTab = newTab;
            
        }

        private void tabControl2_TabRemoved(object sender, EventArgs e)
        {
            //if (isStandAlone && session != null)
            //{
            //    session.Dispose();
            //}
        }

        private void buttonItem22_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Simpleproduct";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Simpleproduct");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;


            _usfloderproduct = new Usfloderproduct(tabControl2, newTab, session, User);

            panel.Controls.Add(_usfloderproduct);
            tabControl2.SelectedTab = newTab;
        }

        private void tabItem1_Click(object sender, EventArgs e)
        {
          
        }

        private void tabControl2_SelectedTabChanged(object sender, TabStripTabChangedEventArgs e)
        {
            if (session == null || !session.IsOpen)
            {
                if (session != null)
                {
                    session.Dispose();
                }
                session = ApplicationContext.SessionFactory.OpenSession();
                session.FlushMode = FlushMode.Never;
            }
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void setup_Click(object sender, EventArgs e)
        {

        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Customer";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Customer");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;


            _uscustomer = new Uscustomer(tabControl2, newTab, session,User);

            panel.Controls.Add(_uscustomer);
            tabControl2.SelectedTab = newTab;

        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Simplefloder";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Simplefloder");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;


            _ussimplefloder = new Ussimplefloder(tabControl2, newTab, session, User);

            panel.Controls.Add(_ussimplefloder);
            tabControl2.SelectedTab = newTab;

        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Outgoing";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Outgoing");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _usoutgoing = new Usoutgoing(tabControl2, newTab, session, User);

            panel.Controls.Add(_usoutgoing);
            tabControl2.SelectedTab = newTab;
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Coa";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Coa");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _uscoa = new Uscoa(tabControl2, newTab, session, User);

            panel.Controls.Add(_uscoa);
            tabControl2.SelectedTab = newTab;
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Addorder";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Addorder");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _usaddorder = new Usaddorder(tabControl2, newTab, session, User);

            panel.Controls.Add(_usaddorder);
            tabControl2.SelectedTab = newTab;
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonItem7_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Printorder";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Printorder");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _usprintorder = new Usprintorder(tabControl2, newTab, session, User);

            panel.Controls.Add(_usprintorder);
            tabControl2.SelectedTab = newTab;
        }

        private void buttonItem8_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "NameplateCore";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("NameplateCore");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _uscore = new Uscore(tabControl2, newTab, session, User);

            panel.Controls.Add(_uscore);
            tabControl2.SelectedTab = newTab;
        }

        private void buttonItem9_Click(object sender, EventArgs e)
        {
            int c = tabControl2.Tabs.Count;
            for (int i = 0; i < c; i++)
            {
                string tabName = "Packing";
                if (tabControl2.Tabs[i].ToString() == tabName)
                {
                    tabControl2.SelectedTab = tabControl2.Tabs[i];
                    return;
                }

            }

            TabItem newTab = tabControl2.CreateTab("Packing");
            TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;

            _uspacking = new Uspacking(tabControl2, newTab, session, User);

            panel.Controls.Add(_uspacking);
            tabControl2.SelectedTab = newTab;

        }

       
    }
}
